package com.yinhe.music.yhmusic.video.list;

import com.hwangjr.rxbus.RxBus;
import com.yinhe.music.event.RxBusEventType;
import com.yinhe.music.yhmusic.base.BaseModel;
import com.yinhe.music.yhmusic.constants.IConstants;
import com.yinhe.music.yhmusic.model.MvsInfo;
import com.yinhe.music.yhmusic.network.response.ResponseTransformer;
import com.yinhe.music.yhmusic.share.SharePresenter;
import com.yinhe.music.yhmusic.utils.UmengEventUtils;

import java.util.List;

import io.reactivex.disposables.Disposable;

import static com.yinhe.music.yhmusic.constants.IConstants.FAV_MV;
import static com.yinhe.music.yhmusic.constants.IConstants.INSTRUMENT;
import static com.yinhe.music.yhmusic.constants.IConstants.INSTRUMENT_MV;
import static com.yinhe.music.yhmusic.constants.IConstants.MOVIE;
import static com.yinhe.music.yhmusic.constants.IConstants.SINGER;

/**
 * @author 何晓华
 */
public class MvListPresenter extends SharePresenter<BaseModel, IMvListContract.IMvListView> implements IMvListContract.IMvListPresenter {
    @Override
    public void getMovieList(int page, int pageSize, int classifyId, int type) {
        if (getView() == null) {
            return;
        }

        getView().showLoading();

        Disposable collectDisposable = mModel.getMvList(page, pageSize, classifyId)
                .compose(ResponseTransformer.handleResult())
                .compose(schedulerProvider.applySchedulers())
                .subscribe(mvsInfo -> {
                    getView().hideLoading();
                    setMovieData(mvsInfo, type);
                }, throwable -> {
                    getView().hideLoading();
                    getView().showMessage(throwable);

                });

        mDisposable.add(collectDisposable);
    }

    @Override
    public void collectMv(MvsInfo mvsInfo, int position) {
        if (getView() == null) {
            return;
        }

        getView().showLoading();

        if (mvsInfo.getCollectStatus() == 0) {
            Disposable disposable = mModel.collect(mvsInfo.getMovieId(), IConstants.MOVIE)
                    .compose(schedulerProvider.applySchedulers())
                    .subscribe(baseInfoResponse -> {
                        updateMvsInfo(mvsInfo, position);
                    }, throwable -> {
                        getView().hideLoading();
                        getView().showMessage(throwable);
                    });
            mDisposable.add(disposable);
        } else {
            Disposable disposable = mModel.unCollect(mvsInfo.getMovieId(), IConstants.MOVIE)
                    .compose(schedulerProvider.applySchedulers())
                    .subscribe(baseInfoResponse -> {
                        updateMvsInfo(mvsInfo, position);
                    }, throwable -> {
                        getView().hideLoading();
                        getView().showMessage(throwable);
                    });
            mDisposable.add(disposable);
        }
    }

    @Override
    public void getSearchList(int page, int pageSize, String keyword, int type) {
        if (getView() == null) {
            return;
        }

        if (page == 1) {
            getView().showLoading();
        }

        Disposable disposable = mModel.getSearchList(page, pageSize, keyword, MOVIE)
                .compose(ResponseTransformer.handleResult())
                .compose(schedulerProvider.applySchedulers())
                .subscribe(searchInfo -> {
                            getView().hideLoading();
                            MvsInfo mvsInfo = new MvsInfo(searchInfo.getPageNum(),searchInfo.getMovieList());
                            setMovieData(mvsInfo, type);
                        },
                        throwable -> {
                            getView().hideLoading();
                            getView().showMessage(throwable);
                        });

        mDisposable.add(disposable);
    }

    @Override
    public void getSingerMovieList(int page, int pageSize, int id, int type) {
        if (getView() == null) {
            return;
        }

        if (page == 1) {
            getView().showLoading();
        }

        String movieType;

        if (type == INSTRUMENT_MV) {
            movieType = INSTRUMENT;
        } else {
            movieType = SINGER;
        }

        addSubscription(mModel.getSingerMvs(movieType, page, pageSize, id)
                .compose(ResponseTransformer.handleResult())
                .compose(schedulerProvider.applySchedulers())
                .subscribe(mvsInfo -> {
                            getView().hideLoading();
                            setMovieData(mvsInfo, type);
                        },
                        throwable -> {
                            getView().hideLoading();
                            getView().showMessage(throwable);
                        }));
    }

    @Override
    public void getCollectMovieList(int page, int pageSize, int type) {
        if (getView() == null) {
            return;
        }

        if (page == 1) {
            getView().showLoading();
        }

        addSubscription(mModel.getCollectMovie(page, pageSize)
                .compose(ResponseTransformer.handleResult())
                .compose(schedulerProvider.applySchedulers())
                .subscribe(mvsInfo -> {
                            getView().hideLoading();
                            setMovieData(mvsInfo, type);
                        },
                        throwable -> {
                            getView().hideLoading();
                            getView().showMessage(throwable);
                        }));
    }

    private void updateMvsInfo(MvsInfo mvsInfo, int position) {
        if (mvsInfo.getCollectStatus() == 0) {
            UmengEventUtils.collect(getView().getContext(), UmengEventUtils.MV);
            mvsInfo.setCollectStatus(1);
            getView().showToast("收藏MV成功");
        } else {
            mvsInfo.setCollectStatus(0);
            getView().showToast("取消收藏MV");
        }

        getView().hideLoading();
        getView().updateCollectUI(mvsInfo, position);

        RxBus.get().post(RxBusEventType.Mv.UPDATA_COLLECT_NUM, String.valueOf(mvsInfo.getMovieId()));
    }

    private void setMovieData(MvsInfo mvsInfo, int type) {
        List<MvsInfo> mMvList = mvsInfo.getMovieList();
        if (mMvList != null) {
            for (int i = 0; i < mMvList.size(); i++) {
                MvsInfo mv = mMvList.get(i);
                if (type == IConstants.NEW_MORE_MV || type == IConstants.MORE_VIDEO) {
                    mv.setItemType(MvsInfo.MORE);
                } else if (type == FAV_MV) {
                    mv.setItemType(MvsInfo.FAV);
                } else {
                    mv.setItemType(MvsInfo.SINGER_DETAIL);
                }
            }

            getView().setMvListUI(mMvList, mvsInfo.getPageNum());

            if (type == FAV_MV) {
                getView().setCollectListHead(mvsInfo.getCollectNum());
            }


        }
    }
}
